/**
  ******************************************************************************
  * File Name          : lorabsp.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#if(COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA)
#include "lora.h"

//#define LORA_UART_TO_TTY    2

/* USART2 init function */
void LoRa_BspInit(void)
{
  timer_parameter_struct timer_initpara;
  
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOE);
	rcu_periph_clock_enable(RCU_SYSCFG);
	rcu_periph_clock_enable(RCU_UART3);
	rcu_periph_clock_enable(LORA_TIMER_CLOCK);
  
  rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
  
	gpio_mode_set(LORA_HOST_GPIO_Port, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, LORA_HOST_Pin);
	syscfg_exti_line_config(EXTI_SOURCE_GPIOC, EXTI_SOURCE_PIN0);
	exti_init(EXTI_0, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
	exti_interrupt_flag_clear(EXTI_0);
	
  gpio_mode_set(LORA_RELOAD_GPIO_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, LORA_RELOAD_Pin);
	gpio_output_options_set(LORA_RELOAD_GPIO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, LORA_RELOAD_Pin);
	
	gpio_mode_set(LORA_PWR_GPIO_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, LORA_PWR_Pin);
	gpio_output_options_set(LORA_PWR_GPIO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, LORA_PWR_Pin);
	
	gpio_mode_set(LORA_RST_GPIO_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, LORA_RST_Pin);
	gpio_output_options_set(LORA_RST_GPIO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, LORA_RST_Pin);
	
	
	/* configure the USART0 Tx pin and USART0 Rx pin */
	gpio_af_set(GPIOA, GPIO_AF_8, GPIO_PIN_0);
	gpio_af_set(GPIOA, GPIO_AF_8, GPIO_PIN_1);
	
	/* configure USART0 Tx as alternate function push-pull */
	gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_0);
	gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);

	/* configure USART0 Rx as alternate function push-pull */
	gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_1);
	gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
	
	/* USART configure */
	usart_deinit(UART3);
	usart_baudrate_set(UART3, 115200u);
	usart_word_length_set(UART3, USART_WL_8BIT);
	usart_parity_config(UART3, USART_PM_NONE);
	usart_stop_bit_set(UART3, USART_STB_1BIT);
	usart_receive_config(UART3, USART_RECEIVE_ENABLE);
	usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
	usart_halfduplex_disable(UART3);
	usart_synchronous_clock_disable(UART3);
//	usart_receiver_timeout_threshold_config(UART3, 10000); /* 200M主频下,115200波特率超时时间约为每个计数8.68us */
//	usart_receiver_timeout_disable(UART3);
	
  
  timer_deinit(LORA_TIMER);
	timer_struct_para_init(&timer_initpara);
	timer_initpara.prescaler         = 40000U;
  timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  timer_initpara.period            = 125;
  timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  timer_initpara.repetitioncounter = 0U;
	timer_init(LORA_TIMER, &timer_initpara);
	timer_auto_reload_shadow_enable(LORA_TIMER);
  timer_single_pulse_mode_config(LORA_TIMER, TIMER_SP_MODE_SINGLE);
  
	timer_interrupt_flag_clear(LORA_TIMER, TIMER_INT_FLAG_UP);
	timer_flag_clear(LORA_TIMER, TIMER_FLAG_UP);
  
  
	nvic_irq_enable(UART3_IRQn, LORA_UART_IRQ_PRIORITY, 0);
	nvic_irq_enable(LORA_TIMER_IRQn, LORA_TIMER_IRQ_PRIORITY, 0);
  nvic_irq_enable(EXTI0_IRQn, LORA_IO_IRQ_PRIORITY, 0);
  
	usart_interrupt_enable(UART3, USART_INT_RBNE);
  usart_interrupt_disable(UART3, USART_INT_RT);
	timer_interrupt_enable(LORA_TIMER, TIMER_INT_UP);
  
  
  timer_disable(LORA_TIMER);
  
	usart_enable(UART3);
}

void LoRa_UartSendStr(const char *buf)
{
	uint32_t i;
  
	for(i=0; buf[i]!='\0'; i++)
	{
    USART_DATA(UART3) = (buf[i]& (uint16_t)0x01FFu);
    while(!(USART_STAT0(UART3) & USART_STAT0_TBE));

#if LORA_UART_TO_TTY > 1
		USART_DATA(UART4) = (buf[i]& (uint16_t)0x01FFU);
    while(!(USART_STAT0(UART4) & USART_STAT0_TBE));
#endif
	}
}

void LoRa_UartSendBuf(const uint8_t *buf, uint32_t len)
{
	uint32_t i;
	
	for(i=0; i<len; i++)
	{
		USART_DATA(UART3) = (buf[i]& (uint16_t)0x01FFu);
    while(!(USART_STAT0(UART3) & USART_STAT0_TBE));

#if LORA_UART_TO_TTY > 1
    USART_DATA(UART4) = (buf[i]& (uint16_t)0x01FFU);
    while(!(USART_STAT0(UART4) & USART_STAT0_TBE));
#endif
	}
}

/**
  * @brief This function handles USART2 global interrupt.
  */
void UART3_IRQHandler(void)
{
	if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_RBNE) != RESET)
	{
    TIMER_CTL0(LORA_TIMER) &= ~(uint32_t)TIMER_CTL0_CEN;
    
		LoRa_RecvIsr.buf[LoRa_RecvIsr.count++] = USART_DATA(UART3);
		
#if LORA_UART_TO_TTY > 1
    USART_DATA(UART4) = USART_DATA(UART3);
    while(!(USART_STAT0(UART4) & USART_STAT0_TBE));
#endif
		
		if(LoRa_RecvIsr.count > LORA_BUF_MAXLEN)
		{
			if(LoRa_Recv_qhd != NULL)
			{
				xQueueSendFromISR(LoRa_Recv_qhd, &LoRa_RecvIsr, NULL);
				LoRa_RecvIsr.count = 0;
			}
		}
    
    TIMER_CNT(LORA_TIMER) = (uint32_t)0;
		TIMER_CTL0(LORA_TIMER) |= (uint32_t)TIMER_CTL0_CEN;
	}
	
//	if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_RT) != RESET)
//	{
//		usart_interrupt_flag_clear(UART3, USART_INT_FLAG_RT);
//  }
}

void LORA_TIMER_IRQHandler(void)
{
	if(timer_interrupt_flag_get(LORA_TIMER, TIMER_INT_FLAG_UP) != RESET)
	{
		timer_interrupt_flag_clear(LORA_TIMER, TIMER_INT_FLAG_UP);

    if(LoRa_Recv_qhd != NULL)
		{
			xQueueSendFromISR(LoRa_Recv_qhd, &LoRa_RecvIsr, NULL);
			LoRa_RecvIsr.count = 0;
		}
	}
}

void EXTI0_IRQHandler(void)
{
  if(exti_interrupt_flag_get(EXTI_0) != RESET)
  {
    EXTI_PD = (uint32_t)EXTI_0;
		
		LED_LORA_COMM_Toggle();
  }
}

#endif /* #if(COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA) */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
